﻿Imports System.Data.OleDb
Imports System.Xml

Public Class list1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If ((Request.QueryString("sc") Is Nothing) Or (Request.QueryString("sv") Is Nothing)) Then
            populateList("", "")
        Else
            populateList(Request.QueryString("sc"), Request.QueryString("sv"))
        End If

    End Sub

    Protected Sub btn_Search_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Search.Click

        Dim searchColumn As String = dd_Search.SelectedValue
        Dim searchValue As String = tb_Search.Text

        Response.Redirect("list.aspx?sc=" + searchColumn + "&sv=" + searchValue)

    End Sub


    Protected Sub CreateRSS(ByVal sqlCommand As String)

        'Create RSS

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()
        Dim fillListCmd As OleDbCommand = New OleDbCommand(sqlCommand, oleDbConn)
        fillListCmd.CommandType = CommandType.Text

        Dim dr = fillListCmd.ExecuteReader()

        ' Create RSS
        Dim location = Request.MapPath("XML") & "/" & "TorontoPictures.xml"

        Dim Writer As XmlTextWriter = New XmlTextWriter(location, Encoding.UTF8)

        Writer.WriteStartDocument()

        Writer.WriteStartElement("rss")
        Writer.WriteAttributeString("version", "2.0")

        Writer.WriteStartElement("title")
        Writer.WriteString("Toronto Pictures")
        Writer.WriteEndElement()

        Writer.WriteStartElement("link")
        Writer.WriteString("https://dusanproject.codeplex.com")
        Writer.WriteEndElement()

        Writer.WriteStartElement("description")
        Writer.WriteString("Pictures from Toronto, Canada")
        Writer.WriteEndElement()

        Writer.WriteStartElement("lastBuildDate")
        Writer.WriteString(Date.Now.ToString())
        Writer.WriteEndElement()

        Writer.WriteStartElement("language")
        Writer.WriteString("en-us")
        Writer.WriteEndElement()

        Writer.WriteStartElement("channel")

        Writer.WriteStartElement("TorontoPictures")

        If dr.HasRows Then

            While (dr.Read())

                Writer.WriteStartElement("TorontoPicture")

                Writer.WriteAttributeString("TorontoPictureName", dr.Item("TorontoPictureName"))

                Writer.WriteAttributeString("Description", dr.Item("Description"))

                Writer.WriteAttributeString("Year", dr.Item("YearTaken"))

                Writer.WriteEndElement()

            End While

        End If

        dr.Close()
        dr = Nothing

        Writer.WriteEndElement()


        Writer.WriteEndDocument()

        Writer.Flush()
        Writer.Close()

    End Sub

    Protected Sub populateList(ByVal searchColumn As String, ByVal searchValue As String)

        If (searchValue = "''") Then
            searchValue = ""
        End If

        Dim fillList As String = "SELECT [ID], [TorontoPictureName], [Description], [YearTaken], [Picture] FROM [TorontoPictures]"

        If (searchColumn <> "") Then
            fillList = fillList + " WHERE " + searchColumn + " LIKE '%" + searchValue + "%'"
        End If

        SqlDataSource1.SelectCommand = fillList
        gw_list.DataBind()

        CreateRSS(fillList)

    End Sub

End Class
